<template>
	<view class="content">
		<uinput v-model="phone" placeholder="请输入手机号" type="tel" labelPosition="top" label="手机号"></uinput>
		<uinput v-model="password" placeholder="请输入密码" type="password" labelPosition="top" label="密码"></uinput>
		<text style="font-size: 30rpx;color: #4f4f4f;">专业：</text>
		<xflSelect :list="colleges" placeholder="请选择专业" @change="change"></xflSelect>
		<uinput v-model="name" placeholder="请输入昵称" type="text" labelPosition="top" label="昵称"></uinput><button type="primary"
		 <uinput v-model="realName" placeholder="请输入真实姓名" type="text" labelPosition="top" label="真实姓名"></uinput><button type="primary"
			 @click="join">注册</button>
	</view>
</template>

<script>
	import uinput from "../../components/poiuy-uinput/uinput.vue";
	import xflSelect from '../../components/xfl-select/xfl-select.vue';
	export default {
		components: {
			uinput,
			xflSelect
		},
		data() {
			return {
				colleges: [],
				realCollege: "",
				phone: "",
				password: "",
				name: "",
				realName: ""
			}
		},
		methods: {
			change(el) {
				this.realCollege = el.newVal
			},
			getColleges() {
				var t = this;
				this.colleges = [];
				uni.request({
					url: "http://localhost:4567/messy_json/colleges.json",
					method: "POST",
					success(ret) {
						for (var i = 0; i < ret.data.length; i++) {
							t.colleges.push({
								value: ret.data[i].collegeName,
								index: ret.data[i].id
							})
						}
					},
				})
			},
			join() {
				var t = this;
				var data = {
					phone: t.phone,
					password: t.password,
					realName: t.realName,
					college: t.realCollege,
					name: t.name
				};
				console.log(data);
				uni.request({
					url: "http://localhost:4567/user_json/join.opera",
					method: "POST",
					data: data,
					success(res) {
						if (res.statusCode != 200 || res.data.code) {
							if (res.data.msg) {
								uni.showModal({
									title: '提示',
									content: res.data.msg,
									success: function(res) {
										if (res.confirm) {} else if (res.cancel) {
											console.log('用户点击取消');
										}
									}
								});
								return;
							}
							uni.showModal({
								title: '提示',
								content: "填写有误",
								success: function(res) {
									if (res.confirm) {} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
							return;
						} else {
							uni.showModal({
								title: '提示',
								content: "注册成功，欢迎您 " + res.data.name,
								success: function(res) {
									if (res.confirm) {
										uni.navigateBack({
											url: "/"
										})
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
							console.log(res.data);
						}
					}
				});
			}
		},
		onLoad() {

			this.getColleges();
		}
	}
</script>

<style>

</style>
